//
//  TEAlertController.m
//  TuringExchange
//
//  Created by LFY on 2017/10/9.
//  Copyright © 2017年 tuling. All rights reserved.
//

#import "TEAlertController.h"

#import "TEAlertPresentationController.h"

@interface TEAlertAnimator : NSObject <UIViewControllerAnimatedTransitioning>

- (instancetype)initWithAlertController:(TEAlertController *)controller;
@property (nonatomic, weak) TEAlertController *alertController;
@property (nonatomic, assign) BOOL isPresent;

@end

@implementation TEAlertAnimator

#pragma mark -
- (instancetype)initWithAlertController:(TEAlertController *)controller {
    self = [super init];
    if (self) {
        _alertController = controller;
    }
    return self;
}

#pragma mark - UIViewControllerAnimatedTransitioning
- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.25;
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    if (_isPresent) {
        [self animatePresentTransation:transitionContext];
    } else {
        [self animateDismissTransation:transitionContext];
    }
}

#pragma mark -
- (void)animatePresentTransation:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    [transitionContext.containerView addSubview:toView];
    if (_alertController.preferredStyle == TEAlertControllerStyleAlert) {
        toView.transform = CGAffineTransformMakeScale(1.2, 1.2);
    } else {
        toView.transform = CGAffineTransformMakeTranslation(0, toView.bounds.size.height);
    }
    transitionContext.containerView.alpha = 0;
    [UIView animateWithDuration:0.25 animations:^{
        toView.transform = CGAffineTransformIdentity;
        transitionContext.containerView.alpha = 1.0;
    } completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
}

- (void)animateDismissTransation:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    [UIView animateWithDuration:0.25 animations:^{
        if (self.alertController.preferredStyle == TEAlertControllerStyleActionSheet) {
            fromView.transform = CGAffineTransformMakeTranslation(0, fromView.bounds.size.height);
        }
        transitionContext.containerView.alpha = 0.0;
    } completion:^(BOOL finished) {
        [fromView removeFromSuperview];
        [transitionContext completeTransition:YES];
    }];
}

@end

@interface TEAlertController ()<UIViewControllerTransitioningDelegate>

@property (nonatomic, strong) TEAlertAnimator *animator;

@end

@implementation TEAlertController

#pragma mark - Setter

#pragma mark - Getter

#pragma mark - Life Cycle
+ (instancetype)alertControllerWithTitle:(NSString *)title
                          preferredStyle:(TEAlertControllerStyle)preferredStyle{
    TEAlertController *alert = [[TEAlertController alloc] init];
    alert.title = title;
    alert.preferredStyle = preferredStyle;
    return alert;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configPresentation];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    [self configPresentation];
}

- (void)configPresentation{
    self.modalPresentationStyle = UIModalPresentationCustom;
    self.transitioningDelegate = self;
    _animator = [[TEAlertAnimator alloc] initWithAlertController:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //[self addNotificationObserver];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - UI

#pragma mark - Notification
- (void)addNotificationObserver {
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self
                           selector:@selector(keyboardWillShow:)
                               name:UIKeyboardWillShowNotification
                             object:nil];
    [notificationCenter addObserver:self
                           selector:@selector(keyboardWillHide:)
                               name:UIKeyboardWillHideNotification
                             object:nil];
}

- (void)keyboardWillShow:(NSNotification *)notification {
    CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    if ((self.view.frame.origin.y+self.view.frame.size.height) < keyboardRect.origin.y - 10) return;
    [UIView animateWithDuration:0.25 animations:^{
        self.view.transform = CGAffineTransformMakeTranslation(0, keyboardRect.origin.y - 10 - (self.view.frame.origin.y+self.view.frame.size.height));
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    [UIView animateWithDuration:0.25 animations:^{
        self.view.transform = CGAffineTransformIdentity;
    }];
}

#pragma mark - Request

#pragma mark - User Event

#pragma mark - UIViewControllerTransitioningDelegate
- (UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(UIViewController *)presenting sourceViewController:(UIViewController *)source {
    return [[TEAlertPresentationController alloc] initWithPresentedViewController:presented presentingViewController:presenting];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    _animator.isPresent = NO;
    return _animator;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    _animator.isPresent = YES;
    return _animator;
}

#pragma mark -


@end
